<template>
  <view class="work-container">
    <u-navbar placeholder autoBack>
      <view slot="center" style="width: 70vw">
        <TopBar :tabBars="tabBars" :tabIndex="tabIndex" @change="onTabChange"></TopBar>
      </view>
    </u-navbar>

    <table-list :listData="listData" :status="tabIndex"></table-list>
  </view>
</template>

<script>
import TopBar from '@/components/top-bar/index.vue'
import CuFilter from '@/components/cu-filter/cu-filter.vue'
import TableList from './components/tableList.vue'
import { mapGetters } from 'vuex'
import { reqAlarmInformationList } from '@/api/alarm-info'
import UNavbar from '@/uni_modules/uview-ui/components/u-navbar/u-navbar.vue'
export default {
  components: {
    UNavbar,
    TopBar,
    TableList,
    CuFilter
  },
  computed: {
    ...mapGetters(['staffId'])
  },
  data() {
    return {
      isFinished: false,
      tabBars: [
        {
          name: '待办',
          componentName: 'all',
          index: 0
        },
        {
          name: '记录',
          componentName: 'mine',
          index: 1
        }
      ],
      tabIndex: 0,
      pageNum: 1,
      total: null,
      listData: [],
      allSearchParams: { enableHandler: '0' }
    }
  },
  // 筛选按钮点击
  onNavigationBarButtonTap() {
    if (this.tabIndex == 0) {
      this.$refs.allCuFilter.toggle()
    } else {
      this.$refs.myCuFilter.toggle()
    }
  },
  onPullDownRefresh() {
    this.initData()
  },
  onReachBottom() {
    this.pageNum++
    this.getList()
  },
  onShow() {
    this.initData()
  },
  methods: {
    initData() {
      this.pageNum = 1
      this.isFinished = false
      this.getList()
    },
    onTabChange(obj) {
      if (this.tabIndex === obj.index) {
        return
      }
      this.tabIndex = obj.index
      this.listData = []
      this.initData()
    },
    getList() {
      if (this.isFinished) {
        return
      }

      this.allSearchParams.enableHandler = `${this.tabIndex}`
      this.getAll()
    },
    getAll() {
      const req = {
        pageNum: this.pageNum,
        pageSize: 10,
        ...this.allSearchParams
      }
      reqAlarmInformationList(req)
        .then((res) => {
          this.listData = this.pageNum === 1 ? res.rows : [...this.listData, ...res.rows]
          this.isFinished = this.listData.length === res.total
        })
        .finally(() => {
          uni.stopPullDownRefresh()
        })
    }
  }
}
</script>
